<?php

namespace Admin\Controller;

use User\Api\UserApi;
use Org\Util\Sync;

/**
 * 热门搜索控制器
 * @author zhangwei
 * 2015-06-24
 */
class SearchKeyController extends AdminController {

    /**
     * 热门搜索词列表
     * @author zhangwei
     * 2015-06-24
     */
    public function index() {
        $title = I('title');
        $sql = "select a.* from gr_searchkey a where a.status = 1 and a.sname like '%" . $title . "%'";
        $model = new \Think\Model();
        $list = $model->query($sql);
        int_to_string($list);

        $this->assign('_list', $list);
        $this->meta_title = '数据列表';
        $this->display();
    }


    /**
     * 新增热门搜索词
     * @author zhangwei
     * 2015-06-24
     */
    public function add($sName = "") {
        if (IS_POST) {
            $data = array('sname' => $sName, 'createdate' => NOW_TIME, 'lastupdate' => NOW_TIME);
            $id = M('searchkey')->add($data);
            if (!$id) {
                $this->error("添加失败！");
            } else {
                $this->success('添加成功！', U('index'));
            }
        } else {
            $this->display();
        }
    }


    /**
     * 编辑热门词
     * @author zhangwei
     * 2015-06-24
     */
    public function edit($sName = "") {
        $id = I('id');
        if (IS_POST) {

            $syncStatus = M('searchkey')->where(array('id' => $id))->getField('syncStatus');
            if ($syncStatus == 0) {
                $syncStatus = 2;
            } else {
                $syncStatus = 1;
            }

            $data = array('sname' => $sName, 'lastupdate' => NOW_TIME, 'syncStatus' => $syncStatus);

            if (!M('searchkey')->where(array('id' => $id))->save($data)) {
                $this->error("修改失败！");
            } else {
                $this->success('修改成功！', U('index'));
            }
        } else {
            $data = M('searchkey')->where(array('id' => $id))->find();
            $this->data = $data;
            $this->display();
        }
    }
    
    /**
     * 删除热门搜索词
     * @author zhangwei
     * 2015-06-24
     */
    public function del($id = 0) {
        if ($id) {
            $seckill = M("searchkey");

            $data = array('status' => -1, 'syncStatus' => 3);
            $result = $seckill->where(array('id' => $id))->save($data);

            if ($result) {
                $this->success('删除成功！', U('index'));
            } else {
                $this->error("删除失败！");
            }
        }
    }
    
    /**
     * 上推热门搜索词
     * @author zhangwei
     * 2015-06-24
     */
    public function push() {
        $reuslt = D("SearchKeyLeanCloud")->push();
        $this->success('热门搜索词数据上推成功！', U('index'), true);
    }
}
